<template>
  <div class="layout-container">
    <el-container>
      <el-aside width="200px">
        <el-menu
          :router="true"
          :default-active="$route.path"
          class="el-menu-vertical"
        >
          <el-menu-item index="/books">
            <el-icon><Document /></el-icon>
            <span>图书管理</span>
          </el-menu-item>
          <el-menu-item
            v-if="isAdmin"
            index="/users"
          >
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/borrowing">
            <el-icon><List /></el-icon>
            <span>借阅记录</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <el-container>
        <el-header>
          <div class="header-content">
            <h2>图书馆管理系统</h2>
            <div class="user-info">
              <span>{{ username }}</span>
              <el-button type="text" @click="handleLogout">退出</el-button>
            </div>
          </div>
        </el-header>
        
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { Document, User, List } from '@element-plus/icons-vue'

export default {
  components: {
    Document,
    User,
    List
  },
  
  setup() {
    const store = useStore()
    const router = useRouter()
    
    const username = computed(() => store.state.user?.username)
    const isAdmin = computed(() => 
      store.state.user?.roles.some(role => role.name === 'ROLE_ADMIN')
    )
    
    const handleLogout = () => {
      store.commit('logout')
      router.push('/login')
    }
    
    return {
      username,
      isAdmin,
      handleLogout
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.el-aside {
  background-color: #304156;
}

.el-menu {
  border-right: none;
}
</style> 